//
// Created by 王书哲 on 25-3-20.
// 将源文件压缩为 1/3
#include <stdio.h>
#include <stdlib.h> // exit() 函数原型
#include <string.h> // strcpy(),strcat()函数原型
#define LEN 40
int main(int argc, char *argv[])
{
    FILE *in, *out;
    int ch;
    char name[LEN];
    int count = 0;
    if (argc < 2)
    {
        fprintf(stderr, "Usage: %s filename\n", argv[0]);
        exit(EXIT_FAILURE);
    }
    //设置输入
    if ((in = fopen(argv[1], "r")) == NULL)
    {
        fprintf(stderr, "I couldn't open the file \"%s\"\n", argv[1]);
        exit(EXIT_FAILURE);
    }
    strncpy(name, argv[1], LEN - 5);//拷贝文件名
    name[LEN - 5] = '\0';
    //文件名后添加.red
    strcat(name, ".red");
    if ((out = fopen(name, "w")) == NULL)
    {
        fprintf(stderr, "Can't create output file.\n");
        exit(3);
    }
    while((ch = getc(in)) != EOF)
        if (count++ % 3 == 0)
            //打印三个字符中的第一个字符
            putc(ch, out);
    //收尾工作
    if (fclose(in) != 0 || fclose(out) != 0)
        fprintf(stderr, "Error in closing files\n");
    return 0;
}